package com.hsmpay.mobile.service.bank.city.impl;

import com.hsmpay.common.pojo.BasePojo;
import com.hsmpay.common.service.impl.BaseServiceSupport;
import com.hsmpay.mobile.dao.bank.city.BankCityDao;
import com.hsmpay.mobile.dao.bank.city.BankProvinceDao;
import com.hsmpay.mobile.pojo.bank.city.BankCity;
import com.hsmpay.mobile.pojo.bank.city.BankProvince;
import com.hsmpay.mobile.service.bank.city.BankCityService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

/**
 * 银行城市业务接口 bankCityService
 * 对应数据层 bankCityDao bankProvinceDao
 *
 * @param <Entity>
 * @param <Query>
 * @author 颜铃璋
 * @date 2013-5-20
 */
@Repository("bankCityService")
public class BankCityServiceImpl extends BaseServiceSupport<BasePojo, Long> implements BankCityService<BasePojo, Long> {
    @Resource(name = "bankCityDao")
    protected BankCityDao<BankCity, Long> bankCityDao;
    @Resource(name = "bankProvinceDao")
    protected BankProvinceDao<BankProvince, Long> bankProvinceDao;

    /**
     * 导入万三银行城市  数据
     *
     * @param xmlRealPath
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public int importDataByXml(String xmlRealPath) throws Exception {
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read("D:\\ylz\\20130304.xml");
            Element root = document.getRootElement();
            Element cityElement = root.element("CNAPS_CITY_DATA");
            BankCity bankCity = null;
            BankProvince bankProvince = null;

            // 遍历根结点root的所有子节点element
            int sort = 1;
            int provinceSort = 1;
            int isSecond = 0;
            Long provinceId = 1L;
            for (Iterator itElement = cityElement.elementIterator(); itElement.hasNext(); ) {
                bankCity = new BankCity();
                Element element = (Element) itElement.next();//省节点

                if (null == element.element("CNAPS_CITY_NAME")) {
                    continue;
                }
                String name = element.element("CNAPS_CITY_NAME").getText();//省节点对应的中文名
                String code = element.element("CNAPS_CITY_CODE").getText();//省节点对应的值
                String type = element.element("CNAPS_CITY_TYPE").getText();//类型
                String remark = element.element("CNAPS_CITY_REMARK").getText();//标签


                if (remark.endsWith("省会")) {
                    sort = 1;
                    bankProvince = new BankProvince();
                    bankProvince.setName(remark.substring(0, remark.length() - 1));
                    bankProvince.setSort(sort);
                    provinceId = bankProvinceDao.insertEntity(bankProvince);
                    provinceSort++;
                } else if (remark.endsWith("直辖市")) {
                    sort = 1;
                    bankProvince = new BankProvince();
                    bankProvince.setName(name);
                    bankProvince.setSort(sort);
                    provinceId = bankProvinceDao.insertEntity(bankProvince);
                    provinceSort++;
                }/*else{
                    int intConde = Integer.parseInt(code);
					if(intConde%10 == 0){
						log.debug(code+" ==> "+name);
					}
				}*/
                bankCity.setName(name);
                bankCity.setCode(Integer.parseInt(code));
                bankCity.setType(Integer.parseInt(type));
                bankCity.setRemark(remark);
                bankCity.setSort(sort);
                bankCity.setProvinceId(provinceId);
                if (bankCity.getCode() % 10 == 0) {
                    isSecond = 1;
                }
                bankCity.setIsSecond(isSecond);
                bankCityDao.insertEntity(bankCity);
                isSecond = 0;
                sort++;
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        return 1;
    }

    /**
     * 导出新的 xml
     *
     * @param xmlRealPath
     * @return
     * @throws Exception
     */
    public int expotDataToXml(String xmlRealPath) throws Exception {
        List<BankProvince> bankProvinceList = getBankProvinceList();
        Document doc = DocumentHelper.createDocument();
        //创建数据指令对象
        // doc.addProcessingInstruction("xml-stylesheet", "type = 'text/xsl' href = 'students.xsl'");

        //处理指令没有执行
        //Element root = doc.addElement("students");

        //创建根元素
        Element root = DocumentHelper.createElement("root");
        doc.setRootElement(root);

        Element element = null;
        Element pName = null;
        Element pValue = null;//这里保存 p ID
        List<BankCity> bankCityList = null;
        Element children = null;
        Element child = null;
        Element cName = null;
        Element cValue = null;
        for (BankProvince bankProvince : bankProvinceList) {
            element = root.addElement("element");
            pName = element.addElement("name");
            pValue = element.addElement("value");
            children = element.addElement("children");

            pName.setText(bankProvince.getName());
            pValue.setText(bankProvince.getId().toString());

            bankCityList = getBankSecondCityList(bankProvince.getId());
            for (BankCity bankCity : bankCityList) {
                child = children.addElement("child");
                cName = child.addElement("name");
                cValue = child.addElement("value");

                cName.setText(bankCity.getName());
                cValue.setText(bankCity.getCode().toString());
            }

        }
		  
		  /*//学生信息节点对象
		  Element eleStu1 =root.addElement("student");
		  Element eleName1 = eleStu1.addElement("name");
		  Element eleAge1 = eleStu1.addElement("age");
		  //设置文本内容
		  eleName1.setText("张三");
		  eleAge1.setText("18");
		  //创建第二个学生信息
		  Element eleStu2 = root.addElement("student");
		  Element eleName2 = eleStu2.addElement("name");
		  Element eleAge2 = eleStu2.addElement("age");
		  
		  eleName2.setText("李四");
		  eleAge2.setText("20");*/
        //设置显示
        OutputFormat opf = new OutputFormat("", true);
        opf.setEncoding("UTF-8");

        //输出
        try {
            //   doc.write(pw);
            //   pw.close();
            XMLWriter xmlw = new XMLWriter(opf);
            xmlw.write(doc);
            String filePath = this.getClass().getResource("").getPath();
            String basePath = filePath.substring(0, filePath.indexOf("WEB-INF")) +"WEB-INF/classes/bankCardType.xls" ;
            File file = new File("D:\\city.xml");
            FileWriter fw = new FileWriter(file);
            XMLWriter output = new XMLWriter(fw);
            output.write(doc);
            output.close();
        } catch (Exception e) {

        }
        return 1;
    }

    /**
     * 获得所有的省
     *
     * @return
     * @throws Exception
     */
    public List<BankProvince> getBankProvinceList() throws Exception {
        BankProvince bankProvince = new BankProvince();
        return bankProvinceDao.searchEntityList(bankProvince);
    }

    /**
     * 获得所有 省级下面的第一级
     *
     * @return
     * @throws Exception
     */
    public List<BankCity> getBankSecondCityList() throws Exception {
        BankCity bankCity = new BankCity();
        bankCity.setIsSecond(1);
        return bankCityDao.searchEntityList(bankCity);
    }

    /**
     * 获得所有 省级下面的第一级
     *
     * @param provinceId
     * @return
     * @throws Exception
     */
    public List<BankCity> getBankSecondCityList(Long provinceId) throws Exception {
        BankCity bankCity = new BankCity();
        bankCity.setIsSecond(1);
        bankCity.setProvinceId(provinceId);
        return bankCityDao.searchEntityList(bankCity);
    }

    //---------------------set get start--------------------
    //---------------------set get end----------------------
}
